import request from '@/utils/request';
import { I_CourseRes } from '@/types';

export const updateCourseInformation = (data: I_CourseRes): Promise<any> => {
    return new Promise((_res, _rej) => {
        request<any>({
            url: `course`,
            method: "post",
            data
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '更新成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const getAllCourseInformation = (data: { index: number, size: number; }): Promise<{ total: number; records: I_CourseRes[]; }> => {
    return new Promise((_res, _rej) => {
        request<{ total: number; records: I_CourseRes[]; }>({
            url: `course/page?pageIndex=${data.index}&pageSize=${data.size}`,
            method: "get",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '获取信息成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const deleteCourseInformation = (data: { id: number; }): Promise<I_CourseRes[]> => {
    return new Promise((_res, _rej) => {
        request<I_CourseRes[]>({
            url: `course/${data.id}`,
            method: "delete",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '删除成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const getCourseData = (data: { search: string, index: number, size: number; }): Promise<{ total: number; records: I_CourseRes[]; }> => {
    console.group(11111);
    return new Promise((_res, _rej) => {
        request<{ total: number; records: I_CourseRes[]; }>({
            url: `course/search?searchInput=${data.search}&pageIndex=${data.index}&pageSize=${data.size}`,
            method: "get",
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '查询成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export const addCourseData = (data: I_CourseRes): Promise<I_CourseRes> => {
    return new Promise((_res, _rej) => {
        request<I_CourseRes>({
            url: `course`,
            method: "post",
            data
        }).then(res => {
            if (+res.code !== 200) {
                ElMessage.error({
                    message: res.msg,
                });
                return _rej(res.msg);
            }
            ElMessage.success({
                message: res.msg || '新增成功',
            });
            _res(res.data);
        }).catch((err: Error) => {
            console.log(err);
            ElMessage.error({
                message: '出现异常错误！',
            });
            _rej(err);
        });;
    });
};
export { };